package com.jwd.config

import org.springframework.core.Ordered
import org.springframework.core.annotation.Order
import org.springframework.web.servlet.ModelAndView
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import javax.servlet.http.HttpSession



/**
 * Created by udi on 2017.3.4.
 */
@Order(Ordered.HIGHEST_PRECEDENCE)
class MyFilter: HandlerInterceptorAdapter(){
//    override fun preHandle(request: HttpServletRequest?, response: HttpServletResponse?, handler: Any?): Boolean {
//        val session = request!!.getSession();
//
//        if (session.getAttribute(MyWebMvcConfig.SESSION_KEY) == null) {
//            return false;
//        }
//        return super.preHandle(request, response, handler)
//    }

    override fun postHandle(request: HttpServletRequest?, response: HttpServletResponse?, handler: Any?, modelAndView: ModelAndView?) {
        try {
            super.postHandle(request, response, handler, modelAndView)
        }
        catch (e:Exception){
            if( response == null) {
                return;
            }
            response.status = 500 ;
            response.contentType="text/plain;charset=utf-8";
            response.writer.write(e.message);
        }
    }
}